#include <iostream>
#include <string>
#include <future>
#include <thread>
#include <stdlib.h>
#include <time.h>

int fun(int i)
{
    std::cout << "fun start" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(10));
    std::cout << "fun end" << std::endl;
    return i;
}
void syncQueue(std::future<int> &fe)
{
    std::cout << "result is " << fe.get() << std::endl;
}
int main()
{
    std::cout << "start" << std::endl;
    // std::thread t(fun, 1);
    // t.join();

    //取代线程创建
    std::future<int> fe = std::async(std::launch::async, fun, 1);
    std::future_status status;
    do
    {
        status = fe.wait_for(std::chrono::seconds(1));
        if (status == std::future_status::deferred)
        {
            std::cout << "deferred" << std::endl;
        }
        else if (status == std::future_status::timeout)
        {
            std::cout << "timeout will end" << std::endl;
        }
        else if (status == std::future_status::ready)
        {
            std::cout << "result is " << fe.get() << std::endl;
        }
    } while (status != std::future_status::ready);

    std::cout << "end" << std::endl;
    return 0;
}
